package cn.edu.ldu.communityqa.communityqa.service;

import cn.edu.ldu.communityqa.communityqa.dao.UserDao;
import cn.edu.ldu.communityqa.communityqa.entity.User;
import cn.edu.ldu.communityqa.communityqa.entity.UserPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    private final UserDao userDao;

    @Autowired
    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    // 通过用户名和密码获取用户
    public User findByUsernameAndPassword(String name, String password) {
        return userDao.findByUsernameAndPassword(name, password);
    }

    public User findById(Long id) {
        return userDao.findById(id);
    }

    public List<User> findAll() {
        return userDao.findAll();
    }

    public UserPage findAll(int page, int limit) {
        UserPage userPage = new UserPage();
        userPage.setCount(userDao.count());
        userPage.setUsers(userDao.findAll(page, limit));
        return userPage;
    }

    public int save(User user) {
        return userDao.save(user);
    }

    public int update(User user) {
        return userDao.update(user);
    }

    public int deleteById(Long id) {
        return userDao.deleteById(id);
    }
}
